<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Other\XimuApi;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\Finance\Loan;
use AuctionCore\Common\Page;
use AuctionCore\System\AuctionException;

/**
 * 贷款收款
 * @author Xmh
 */

class LoanReceivebleController extends BaseController
{
    public function __construct() {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];

        //模板赋值
        $this->assign("emp_id", $this->emp_id);
        $this->Loan = new Loan();
    }

    public function index() {
        $this->display();
    }

    /**
     * 贷款收款列表 -- 过户完成
     */
    public function loan_list(){
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['status'] = I('get.status');
        $params['location_area'] = I('get.location_area',''); //所在地
        $params['loan_no'] = I('get.loan_no/s','','trim'); //贷款编号
        $params['order_no'] = I('get.order_no/s','','trim');    //贷款编号
        $params['contact_person'] = I('get.contact_person/s','','trim');    //车商联系人
        $params['contact_mobile'] = I('get.contact_mobile','','trim');    //车商联系电话
        $params['start_time'] = I('get.start_time');    //开始时间
        $params['end_time'] = I('get.end_time');        //结束时间

        //所在地查询
        if(count($this->bc_city) > 0) {
            $bc_code = array_flip($this->bc_city);
            $bc_code = implode(',', $bc_code);
            $params['bc_city'] = $bc_code;
        }

        $result = $this->Loan->get_loan_list($page, $size, $params);
        $list = empty($result['list']) ? array() : $result['list'];

        //分页
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        //dump($list);
        $show = $Page->show();          // 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('params',$params);
        $this->display();
    }

    /**
     * 贷款收款列表 -- 未过户完成
     */
    public function notransfer_loan_list(){
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['status'] = I('get.status');
        $params['location_area'] = I('get.location_area',''); //所在地
        $params['loan_no'] = I('get.loan_no/s','','trim'); //贷款编号
        $params['order_no'] = I('get.order_no/s','','trim');    //贷款编号
        $params['contact_person'] = I('get.contact_person/s','','trim');    //车商联系人
        $params['contact_mobile'] = I('get.contact_mobile','','trim');    //车商联系电话
        $params['start_time'] = I('get.start_time');    //首付开始时间
        $params['end_time'] = I('get.end_time');        //首付结束时间

        //所在地查询
        if(count($this->bc_city) > 0) {
            $bc_code = array_flip($this->bc_city);
            $bc_code = implode(',', $bc_code);
            $params['bc_city'] = $bc_code;
        }

        $result = $this->Loan->get_notransfer_loan_list($page, $size, $params);
        $list = empty($result['list']) ? array() : $result['list'];

        //分页
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
        //dump($list);
        $show = $Page->show();          // 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('params',$params);
        $this->display('loan_list');
    }


    /**
     * 获取贷款详情
     */
    public function get_loan_info(){
        $xm_id = I('post.xm_id/d');
        if($xm_id < 1){
            $this->helpJsonResult('400','参数信息异常');
        }
        $loan_detail = $this->Loan->get_loan_detail_by_xm_id($xm_id);
        echo json_encode($loan_detail);
    }

    /**
     * 确认放款
     */
    public function loan_verify(){
        $xm_id = I('post.xm_id/d');
        $order_id = I('post.order_id/d');//流水编号
        if($xm_id < 1){
            $this->helpJsonResult('400','参数信息异常');
        }
        $product_id = C('PROD_CAR_OLD_CLCW.0');//产品ID
        $order = new AuOrder($order_id);
        $order_detail = $order->detail();
        $loan_log = $order->get_loan_log($xm_id);
        $data = array();
        $data['xm_list_code'] = $loan_log['xm_list_code'];
        $data['product_id']   = $product_id;
        $data['receive_amt'] = $loan_log['loan_money'] * 100;
        $data['ac_date'] = date('Y-m-d H:i:s');
        $data['receive_ac_no'] = C('RECEIVE_AC_NO.0');
        $data['org_book_no'] = $order_detail['order_no'];
        $ximu = new XimuApi();
        $xm_res = $ximu->_get('ORDER', 'confirmreceiveloans', $data);
        $res = $this->Loan->confirm_loan_verify($xm_id);
        if($res === false){
            $this->helpJsonResult('400','放款失败');
        }
        /**
         * 记录车辆跟踪日志
         */
        $car_action_data = array(
            'action_name'  => '核实放款',
            'emp_id' => $this->emp_id
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($order_detail['car_id'], $car_action_data);
        /**
         * 记录拍品跟踪日志
         */
        $order_action_data = array(
            'action_name'  => '核实放款',
            'emp_id' => $this->emp_id
        );
        \AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($order_id, $order_action_data);
        $this->helpJsonResult('200','放款成功');
    }

}